home *** CD-ROM | disk | FTP | other *** search
/ Mac OS 9 Serial Number Archive / SN Archive 2023.11.04.toast / BSNG / SDK / BSNG SDK 1.0.3 / Example Plugin / Example.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-29  |  10.9 KB  |  461 lines  |  [TEXT/CWIE]

  1. /*
  2.  * $Workfile:: Example.c                                                      $
  3.  * $Revision:: 4                                                              $
  4.  *
  5.  * $Author:: Buck Rogers                                                      $
  6.  * $Modtime:: 29.10.1997 17:41 Uhr                                            $
  7.  *
  8.  * $History:: Example.c                                                       $
  9.  * 
  10.  * *****************  Version 4  *****************
  11.  * User: Buck Rogers  Date: 29.10.1997   Time: 17:42 Uhr
  12.  * Updated in $/BSNG/Plugins/BSNG SDK/Example Plugin
  13.  * example plugin now makes use of the new 'errorText' variable
  14.  * 
  15.  * *****************  Version 3  *****************
  16.  * User: Buck Rogers  Date: 08.10.1997   Time: 02:03 Uhr
  17.  * Updated in $/BSNG/Plugins/BSNG SDK/Example Plugin
  18.  * changed lf to cr in list generation so Mac compatible output is created
  19.  * 
  20.  * *****************  Version 2  *****************
  21.  * User: Buck Rogers  Date: 05.10.1997   Time: 19:24 Uhr
  22.  * Updated in $/BSNG/Plugins/BSNG SDK/Example Plugin
  23.  * Added comments related to the new name, company and numCopies items in the
  24.  * parameter block
  25.  * 
  26.  * *****************  Version 1  *****************
  27.  * User: Buck Rogers  Date: 30.09.1997   Time: 18:31 Uhr
  28.  * Created in $/BSNG/Plugins/BSNG SDK/Example Plugin
  29.  * Adding subproject 'BSNG' to '$/'
  30.  *
  31.  * $NoKeywords::                                                              $
  32.  */
  33.  
  34.  
  35. #include <MixedMode.h>
  36. #include <A4Stuff.h>
  37.  
  38. #include "standard utils.h"
  39. #include "UltraU.h"
  40. #include "BSNG API.h"
  41.  
  42.  
  43. /*    don't touch this __procinfo definition or the calls from native code will crash the machine */
  44.  
  45. ProcInfoType __procinfo = kThinkCStackBased | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(BSNGParamBlockPtr)));
  46.  
  47.  
  48. /*    function prototypes for ANSI C */
  49.  
  50. static void DoInit(BSNGParamBlockPtr inData);
  51. static Boolean DoValidate(BSNGParamBlockPtr inData);
  52. static void DoCalc(BSNGParamBlockPtr inData);
  53. static void DoRandomCalc(BSNGParamBlockPtr inData);
  54. static Boolean DoAddRandomsToList(BSNGParamBlockPtr inData);
  55. static void DoItemHit(BSNGParamBlockPtr inData);
  56. static void DoCleanup(BSNGParamBlockPtr inData);
  57. void main(BSNGParamBlockPtr inData);
  58.  
  59.  
  60. /*    here is where the fun starts :-) */
  61.  
  62. static void DoInit(BSNGParamBlockPtr inData)
  63. {
  64.     StringPtr        aStr = nil;
  65.     
  66.     
  67.     /*    Initialize the Ultra Random Number generator (optional, use MacOS random functions otherwise) */
  68.     
  69.     Ultra_seed1 = inData->randSeed1;
  70.     Ultra_seed2 = inData->randSeed2;
  71.     Ultra_Init();
  72.     
  73.     /*    Give the BSNG App the informations if your plugin can do random calculations and if it can write
  74.         to the serial number list */
  75.     
  76.     inData->wantsRandomButton = true;
  77.     inData->canAddToSNList = true;
  78.     
  79.     /*    Initialize the EditFields with initial default values (optional, they are empty otherwise) */
  80.     /*    if you create a name-based generator please make sure that you copy the name, company and/or numCopies */
  81.     /*    values into the corrosponding fields */
  82.     
  83.     aStr = inData->itemValue[kItemValue1];
  84.     BlockMoveData("\p12345678", aStr, 9);
  85.     
  86.     aStr = inData->itemValue[kItemValue2];
  87.     BlockMoveData("\p23456789", aStr, 9);
  88.     
  89.     aStr = inData->itemValue[kItemValue3];
  90.     BlockMoveData("\p34567890", aStr, 9);
  91.     
  92.     aStr = inData->itemValue[kItemValue4];
  93.     aStr[0] = 0;
  94.     
  95.     /*    Tell the BSNG App if the initialisation was successful (it was in our case) */
  96.     
  97.     inData->errorText[0] = 0;
  98.     inData->error = errExtNoErr;
  99.     inData->errorInItem = 0;
  100. }  /* DoInit */
  101.  
  102.  
  103. static Boolean DoValidate(BSNGParamBlockPtr inData)
  104. {
  105.     StringPtr        num1Str = inData->itemValue[kItemValue1];
  106.     StringPtr        num2Str = inData->itemValue[kItemValue2];
  107.     StringPtr        num3Str = inData->itemValue[kItemValue3];
  108.     
  109.     Str31            inputStr    = "\pThe input for ";
  110.     Str31            tooLongStr    = "\p is too long! ";
  111.     Str31            emptyStr    = "\p is empty! ";
  112.     Str63            pleaseStr    = "\p\rPlease enter a number between 1 and 99999999.";
  113.     
  114.     Boolean            error = false;
  115.     
  116.     
  117.     /*    we do some simple checks here if the edit fields are not empty or not longer than 8 characters, additionally we could
  118.         also check if the values are really only numbers, but I skipped that because the input filter for the edit fileds
  119.         was set to integer anyway (defined in Constructor) */
  120.     
  121.     inData->errorText[0] = 0;
  122.     
  123.     if ((num1Str[0] < 1) || (num1Str[0] > 8))
  124.     {
  125.         /*    if something was wrong in the input tell the BSNG App what edit field contained wrong values */
  126.         
  127.         inData->errorInItem = kEditItem1;
  128.         error = true;
  129.         
  130.         /*    build the errorText string (optional), leave errorText empty if you don't want to use this feature */
  131.         
  132.         myAppendPStr(inData->errorText, inputStr);
  133.         myAppendPStr(inData->errorText, "\pNumber 1");
  134.         
  135.         if (num1Str[0] < 1)
  136.         {
  137.             myAppendPStr(inData->errorText, emptyStr);
  138.         }
  139.         else
  140.         {
  141.             myAppendPStr(inData->errorText, tooLongStr);
  142.         }
  143.         
  144.         myAppendPStr(inData->errorText, pleaseStr);
  145.     }
  146.     else if ((num2Str[0] < 1) || (num2Str[0] > 8))
  147.     {
  148.         inData->errorInItem = kEditItem2;
  149.         error = true;
  150.         
  151.         myAppendPStr(inData->errorText, inputStr);
  152.         myAppendPStr(inData->errorText, "\pNumber 2");
  153.         
  154.         if (num2Str[0] < 1)
  155.         {
  156.             myAppendPStr(inData->errorText, emptyStr);
  157.         }
  158.         else
  159.         {
  160.             myAppendPStr(inData->errorText, tooLongStr);
  161.         }
  162.         
  163.         myAppendPStr(inData->errorText, pleaseStr);
  164.     }
  165.     else if ((num3Str[0] < 1) || (num3Str[0] > 8))
  166.     {
  167.         inData->errorInItem = kEditItem3;
  168.         error = true;
  169.         
  170.         myAppendPStr(inData->errorText, inputStr);
  171.         myAppendPStr(inData->errorText, "\pNumber 3");
  172.         
  173.         if (num3Str[0] < 1)
  174.         {
  175.             myAppendPStr(inData->errorText, emptyStr);
  176.         }
  177.         else
  178.         {
  179.             myAppendPStr(inData->errorText, tooLongStr);
  180.         }
  181.         
  182.         myAppendPStr(inData->errorText, pleaseStr);
  183.     }
  184.     
  185.     if (error)
  186.     {
  187.         inData->error = errExtIncorrectValue;
  188.         return (false);
  189.     }
  190.     
  191.     inData->error = errExtNoErr;
  192.     
  193.     return (true);
  194. }  /* DoValidate */
  195.  
  196.  
  197. static void DoCalc(BSNGParamBlockPtr inData)
  198. {
  199.     StringPtr        num1Str = inData->itemValue[kItemValue1];
  200.     StringPtr        num2Str = inData->itemValue[kItemValue2];
  201.     StringPtr        num3Str = inData->itemValue[kItemValue3];
  202.     StringPtr        addResultStr = inData->itemValue[kItemValue4];
  203.     
  204.     unsigned long    result = 0L;
  205.     long            value = 0L;
  206.     Boolean            error = false;
  207.     short            i = 0;
  208.     
  209.     
  210.     /*    very simply algorithm, just add the 3 numbers and set the result */
  211.     
  212.     StringToNum(num1Str, &value);
  213.     result += value;
  214.     
  215.     StringToNum(num2Str, &value);
  216.     result += value;
  217.     
  218.     StringToNum(num3Str, &value);
  219.     result += value;
  220.     
  221.     NumToString(result, addResultStr);
  222.     
  223.     /*    put the result on the Clipboard so users can copy and paste the serial number into the app they want to register */
  224.     
  225.     addResultStr[addResultStr[0] + 1] = '\0';
  226.     
  227.     ZeroScrap();
  228.     PutScrap(addResultStr[0], 'TEXT', &(addResultStr[1]));
  229. }  /* DoCalc */
  230.  
  231.  
  232. static void DoItemHit(BSNGParamBlockPtr inData)
  233. {
  234.     switch (inData->itemMessage)
  235.     {
  236.         case (300000):
  237.         
  238.             /*    the "About this plugin" button was pressed, do whatever you want, I just display a small Alert here */
  239.         
  240.             NoteAlert(1000, nil);
  241.             break;
  242.         
  243.         default:
  244.             break;
  245.     }
  246. }  /* DoItemHit */
  247.  
  248.  
  249. static void DoRandomCalc(BSNGParamBlockPtr inData)
  250. {
  251.     StringPtr        num1Str = inData->itemValue[kItemValue1];
  252.     StringPtr        num2Str = inData->itemValue[kItemValue2];
  253.     StringPtr        num3Str = inData->itemValue[kItemValue3];
  254.     
  255.     
  256.     /*    create your own random numbers as the random functions are not dependent on user input */
  257.     
  258.     num1Str[0] = 0;                                    /*    empty the string */
  259.     myBaseBRandomPStr(num1Str, 10, 8);                /*    generate 8 random digits */
  260.     
  261.     num2Str[0] = 0;
  262.     myBaseBRandomPStr(num2Str, 10, 8);
  263.     
  264.     num3Str[0] = 0;
  265.     myBaseBRandomPStr(num3Str, 10, 8);
  266.     
  267.     /*    now we are prepared and can call our Calc algorithm */
  268.     
  269.     DoCalc(inData);
  270. }  /* DoRandomCalc */
  271.  
  272.  
  273. static Boolean DoAddRandomsToList(BSNGParamBlockPtr inData)
  274. {
  275.     StringPtr        num1Str = inData->itemValue[kItemValue1];
  276.     StringPtr        num2Str = inData->itemValue[kItemValue2];
  277.     StringPtr        num3Str = inData->itemValue[kItemValue3];
  278.     StringPtr        addResultStr = inData->itemValue[kItemValue4];
  279.     
  280.     long            count = 0L;
  281.     short            i = 0;
  282.     OSErr            err = noErr;
  283.     
  284.     
  285.     /*    write your header to the number list */
  286.     
  287.     count = 24L;
  288.     err = FSWrite(inData->outputRefNum, &count, "BSNG SDK Example Plugin\r");
  289.     
  290.     /*    return false if an error occured, the creation of list will then be completly aborted */
  291.     
  292.     if (err != noErr)
  293.     {
  294.         return (false);
  295.     }
  296.     
  297.     count = 24L;
  298.     err = FSWrite(inData->outputRefNum, &count, "=======================\r");
  299.     
  300.     if (err != noErr)
  301.     {
  302.         return (false);
  303.     }
  304.     
  305.     /*    create numOfListNumbers random serial numbers and write them to the list */
  306.     /*    if you create a name-based generator please make sure that you create at least one number */
  307.     /*    using the name, company and/or numCopies informations. You could create more name-based numbers */
  308.     /*    by using your own name/company database */
  309.     
  310.     for (i = 0; i < inData->numOfListNumbers; i++)
  311.     {
  312.         DoRandomCalc(inData);
  313.         
  314.         count = (long) num1Str[0];
  315.         err = FSWrite(inData->outputRefNum, &count, &(num1Str[1]));
  316.         
  317.         if (err != noErr)
  318.         {
  319.             return (false);
  320.         }
  321.     
  322.         count = 3L;
  323.         err = FSWrite(inData->outputRefNum, &count, " + ");
  324.         
  325.         if (err != noErr)
  326.         {
  327.             return (false);
  328.         }
  329.         
  330.         count = (long) num2Str[0];
  331.         err = FSWrite(inData->outputRefNum, &count, &(num2Str[1]));
  332.         
  333.         if (err != noErr)
  334.         {
  335.             return (false);
  336.         }
  337.     
  338.         count = 3L;
  339.         err = FSWrite(inData->outputRefNum, &count, " + ");
  340.         
  341.         if (err != noErr)
  342.         {
  343.             return (false);
  344.         }
  345.         
  346.         count = (long) num3Str[0];
  347.         err = FSWrite(inData->outputRefNum, &count, &(num3Str[1]));
  348.         
  349.         if (err != noErr)
  350.         {
  351.             return (false);
  352.         }
  353.     
  354.         count = 3L;
  355.         err = FSWrite(inData->outputRefNum, &count, " = ");
  356.         
  357.         if (err != noErr)
  358.         {
  359.             return (false);
  360.         }
  361.         
  362.         count = (long) addResultStr[0];
  363.         err = FSWrite(inData->outputRefNum, &count, &(addResultStr[1]));
  364.         
  365.         if (err != noErr)
  366.         {
  367.             return (false);
  368.         }
  369.     
  370.         count = 1L;
  371.         err = FSWrite(inData->outputRefNum, &count, "\r");
  372.         
  373.         if (err != noErr)
  374.         {
  375.             return (false);
  376.         }
  377.     }
  378.     
  379.     /* don't forget this last newline */
  380.     
  381.     count = 1L;
  382.     err = FSWrite(inData->outputRefNum, &count, "\r");
  383.     
  384.     if (err != noErr)
  385.     {
  386.         return (false);
  387.     }
  388.     
  389.     return (true);
  390. }  /* DoAddRandomsToList */
  391.  
  392.  
  393. static void DoCleanup(BSNGParamBlockPtr inData)
  394. {
  395. #pragma unused (inData)
  396.  
  397.     /*    we didn't allocate any memory in DoInit, so we can leave this empty, otherwise it would be a VERY good idea
  398.         to deallocate/dispose etc. everything we allocated during our work. If you don't do that we have a nice memory leak */
  399.     
  400. }  /* DoCleanup */
  401.  
  402.  
  403. void main(BSNGParamBlockPtr inData)
  404. {
  405. #if (!GENERATINGPOWERPC)
  406.     EnterCodeResource();
  407. #endif
  408.     
  409.     /*    the message dispatcher */
  410.     
  411.     switch(inData->theMessage)
  412.     {
  413.         case (msgExtInit):
  414.             DoInit(inData);
  415.             break;
  416.         
  417.         case (msgExtCalcHit):
  418.         
  419.             if (DoValidate(inData))
  420.             {
  421.                 DoCalc(inData);
  422.             }
  423.             
  424.             break;
  425.         
  426.         case (msgExtRandomHit):
  427.             DoRandomCalc(inData);
  428.             break;
  429.         
  430.         case (msgExtCreateRandom):
  431.             
  432.             /*    report to the BSNG App if your list entry was written ok or if we had an error */
  433.             
  434.             if (DoAddRandomsToList(inData))
  435.             {
  436.                 inData->error = errExtNoErr;
  437.             }
  438.             else
  439.             {
  440.                 inData->error = errExtWritingToList;
  441.             }
  442.             
  443.             break;
  444.         
  445.         case (msgExtItemHit):
  446.             DoItemHit(inData);
  447.             break;
  448.         
  449.         case (msgExtCleanup):
  450.             DoCleanup(inData);
  451.             break;
  452.         
  453.         default:
  454.             break;
  455.     }
  456.     
  457. #if (!GENERATINGPOWERPC)
  458.     ExitCodeResource();
  459. #endif
  460. }  /* main */
  461.